from wtforms import StringField

from poker_bot.admin_panel.app.views.base import MyBaseModelView


class AdminUserBaseModelview(MyBaseModelView):
    column_exclude_list = "password"

    column_details_exclude_list = "password"

    column_export_exclude_list = "password"

    column_list = (
        "email",
        "name",
        "can_edit",
        "can_export",
        "can_delete",
        "can_add",
    )

    column_labels = {
        "email": "Почта",
        "new_password": "Новый пароль",
        "name": "Имя",
        "can_edit": "Может редактировать записи",
        "can_export": "Может экспортировать записи",
        "can_delete": "Может удалять записи",
        "can_add": "Может создавать записи",
    }

    form_excluded_columns = ("password", "created_at", "updated_at")

    form_extra_fields = {"new_password": StringField("Новый пароль")}

    column_searchable_list = column_list

    def on_model_change(self, form, model, is_created):
        if len(form.new_password.data) > 0:
            model.set_password(form.new_password.data)
        super().on_model_change(form, model, is_created)
